/*
 * @Author: Jayken
 * @Email: jaykenxie@outlook.com
 * @Date: 2019-08-06 14:03:22
 */

export function templateColumns() {
  const vm = this;
  const COLUMN_WIDTH = window.RPConfig.COLUMN_WIDTH
  return [
    {
      title: vm.$t("module.extend.templateCategory"),
      key: "templateCategory",
      minWidth: COLUMN_WIDTH.COLUMN_WIDTH_4_WORDS,
      maxWidth: COLUMN_WIDTH.COLUMN_WIDTH_4_WORDS + COLUMN_WIDTH.MAX_WIDTH_MORE,
      render: (h, params) => {
        return h(
          "div",
          {
            attrs: {
              class: "ivu-table-cell ivu-table-cell-ellipsis",
              title: this.templateList[params.row.templateCategory]
            }
          },
          this.templateList[params.row.templateCategory]
        );
      }
    },
    {
      title: vm.$t("module.extend.templateCode"),
      key: "templateCode",
      sortable: true,
      minWidth: 150
    },
    {
      title: vm.$t("module.extend.templateName"),
      key: "templateName",
      sortable: true,
      minWidth: 150
    },
    {
      title: vm.$t("module.extend.templateVersion"),
      key: "templateVersion",
      align: "center",
      minWidth: COLUMN_WIDTH.COLUMN_WIDTH_5_WORDS,
      maxWidth: COLUMN_WIDTH.COLUMN_WIDTH_5_WORDS + COLUMN_WIDTH.MAX_WIDTH_MORE,
    },
    {
      title: vm.$t("module.extend.status"),
      align: "center",
      minWidth: COLUMN_WIDTH.COLUMN_WIDTH_4_WORDS,
      maxWidth: COLUMN_WIDTH.COLUMN_WIDTH_4_WORDS + COLUMN_WIDTH.MAX_WIDTH_MORE,

      render: (h, { row }) => h("span", statusFilter(row.status,vm))
    },
    {
      title: vm.$t("common.operating"),
      fixed: "right",
      width: 100,
      render: (h, { row }) => {
        const data = [
          {
            type: "icon",
            icon: row.status === 2 ? "ios-eye" : "md-create",
            text: vm.$t(row.status === 2 ? "common.detail" : "common.edit"),
            handle: () => vm.addHandler(row)
          }
        ];

        let copyBtn = {
          type: "icon",
          icon: "md-copy", 
          text: vm.$t("module.extend.copy"), 
          handle: () => vm.handleCopyTemplate(row)
        }
        let deleteBtn = {
          type: "icon",
          icon: "ios-trash",
          text: vm.$t("common.delete"),
          handle: () => vm.deleteHandler(row)
        }
        if (row.status === 0) { 
          data.push(deleteBtn);
        }
        if (row.status === 1) {
          data.push(copyBtn)
        }
        return h("RowTools", {
          props: { data }
        });
      }
    }
  ];
}

export function addTmpColumns() {
  const vm = this;
  const colms = [
    {
      title: vm.$t("module.extend.type"),
      // key: "type"
      render: (h, { row }) => h("span", getAttrType(row.type,vm))
    },
    {
      title: vm.$t("module.extend.field"),
      key: "field"
    },
    {
      title: vm.$t("module.extend.title"),
      key: "title"
    },
    {
      title: vm.$t("module.extend.value"),
      key: "value"
    }
  ];
  if (vm.formData.status != 2) {
    colms.push({
      title: vm.$t("common.operating"),
      align: "left",
      fixed: "right",
      width: 80,
      render: (h, { row }) => {
        return h("RowTools", {
          props: {
            data: [
              {
                type: "icon",
                icon: "md-create",
                text: vm.$t("common.edit"),
                handle: () => vm.attrEditHandler(row)
              },
              {
                type: "icon",
                icon: "ios-trash",
                text: vm.$t("common.delete"),
                handle: () => vm.attrDeleteHandler(row)
              }
            ]
          }
        });
      }
    });
  }
  return colms;
}

export function optionsColumns() {
  const vm = this;
  return [
    {
      title: vm.$t("module.extend.label"),
      key: "label"
    },
    {
      title: vm.$t("module.extend._value"),
      key: "value"
    },
    {
      title: vm.$t("module.extend.disabled"),
      key: "disabled",
      render: (h, { row }) =>
        h("span", row.disabled ? vm.$t("module.extend.disabled") : vm.$t("module.extend.nodisabled"))
    },
    {
      title: vm.$t("common.operating"),
      align: "left",
      fixed: "right",
      width: 80,
      render: (h, { row }) => {
        return h("RowTools", {
          props: {
            data: [
              {
                type: "icon",
                icon: "md-create",
                text: vm.$t("common.edit"),
                handle: () => vm.addOptHandler(row)
              }, 
              {
                type: "icon",
                icon: "ios-trash",
                text: vm.$t("common.delete"),
                handle: () => vm.optDeleteHandler(row)
              }
            ]
          }
        });
      }
    }
  ];
}

function statusFilter(state,vm) {
  switch (`${state}`) {
    case "0":
      return vm.$t('module.extend.draft');
    case "1":
      return vm.$t('module.extend.issue');
    case "2":
      return vm.$t('module.extend.pigeonhole');
    default:
      return vm.$t('module.extend.draft');
  }
}
// "validate": [{ "required": true, "message": "请输入goods_name", "trigger": "blur" }]
function getAttrType(type,vm) {
  switch (type) {
    case "input":
      return vm.$t('module.extend.input');
    case "InputNumber":
      return vm.$t('module.extend.numInput');
    case "radio":
      return vm.$t('module.extend.radio');
    case "checkbox":
      return vm.$t('module.extend.checkbox');
    case "select":
      return vm.$t('module.extend.select');
    default:
      break;
  }
}
